package com.atkxp.service;

import com.atkxp.dao.UserMapper;
import com.atkxp.pojo.Page;
import com.atkxp.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author user
 * @declaration
 * @create 2022/11/1-20:20
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public int registUser(User user) {
         return userMapper.savaUser(user);
    }

    @Override
    public User login(String name, String password) {
        return userMapper.queryUserByNameAndPassword(name, password);
    }

    @Override
    public boolean existUserName(String name) {
        if(userMapper.queryUserByName(name) == null){
            return false;
        }
        return true;
    }

    @Override
    public User showUserInfoByName(String name) {
        return userMapper.queryUserByName(name);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public Integer setHeadImg(User user) {
        return userMapper.setHeadImg(user);
    }

    @Override
    public User queryUsrById(int id) {
        return userMapper.queryUsrById(id);
    }

    @Override
    public Page<User> getSubscribeUser(Map map) {
        int pageNum = (int)map.get("pageNum");
        int pageSize = (int)map.get("pageSize");
        Page<User> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        Long cnt1 =  userMapper.getFollowCount((int)map.get("userId"));
        long cnt2 = cnt1;
        int bookForPageTotalCount = (int)cnt2;
        page.setPageTotalCount((int)(bookForPageTotalCount));
        page.setPageTotalPage((bookForPageTotalCount % pageSize == 0 ? bookForPageTotalCount / pageSize : bookForPageTotalCount / pageSize + 1));
        int begin = (pageNum - 1) * pageSize;
        map.put("begin", begin);
        List<Integer> subscribeUser = userMapper.getSubscribeUser((int)map.get("userId"));
        List<User> users = new ArrayList<>();
        for(Integer i : subscribeUser){
            users.add(userMapper.queryUsrById(i));
        }
        page.setItem(users);
        return page;
    }

    @Override
    public int subscribe(int userId) {
        return userMapper.subscribe(userId);
    }

    @Override
    public int unsubscribe(int userId) {
        return userMapper.unsubscribe(userId);
    }

    @Override
    public int subscribeAddRelation(int userId, int followUserId, Date followDate) {
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("followUserId", followUserId);
        map.put("followDate", followDate);
        return userMapper.subscribeAddRelation(map);
    }

    @Override
    public int unsubscribeDeleteRelation(int userId, int followUserId) {
        return userMapper.unsubscribeDeleteRelation(userId, followUserId);
    }

    @Override
    public boolean existFollowRelationship(int userId, int followUserId) {
        if(userMapper.existFollowRelationship(userId, followUserId) == 1){
            return true;
        }
        return false;
    }

    @Override
    public Long getFollowCount(int followUserId) {
        return userMapper.getFollowCount(followUserId);
    }
}
